package com.xzzz.irda.auth.core.util;


import com.xzzz.common.base.util.security.SHA256Util;
import com.xzzz.irda.auth.core.token.PasswordEncoder;

/**
 * SHA256 hash 算法
 *
 * @author wangyf
 * 
 */
public class SHA256PasswordEncoder implements PasswordEncoder {

    /**
     * 密码加密
     * sha256(password + salt))
     *
     * @param rawPassword 原始密码
     * @return 密文
     */
    @Override
    public String encode(CharSequence rawPassword) {
        return SHA256Util.encode((String) rawPassword);
    }

    /**
     * 密码匹配
     * @param rawPassword 原始密码，未加密的密码
     * @param encodedPassword 已经加密过的密码
     * @return true: 密码正确
     */
    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        return this.encode(rawPassword).equals(encodedPassword);
    }

    /**
     * sha256(password + salt))
     */
    public static void main(String[] args) {
        String password = "xzzz";
        SHA256PasswordEncoder sha256PasswordEncoder = new SHA256PasswordEncoder();
        System.out.println(sha256PasswordEncoder.encode("xzzzsuJ46iZPAlMnJbnIONBY"));
    }
}
